{% extends "base.html" %}

{% block content %}
     <!-- 添加账号筛选表单 -->
    <div class="row mb-4">
        <div class="col-md-6">
            <form class="row g-3" method="get">
                <div class="col-md-8">
                    <select class="form-select" name="account">
                        <option value="">所有账号</option>
                        {% for account in all_accounts %}
                        <option value="{{ account }}"
                                {% if account_filter == account %}selected{% endif %}>
                            {{ account }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-4">
                    <button type="submit" class="btn btn-primary">筛选</button>
                    {% if account_filter %}
                    <a href="/clear_filter" class="btn btn-secondary ms-2">清除</a>
                    {% endif %}
                </div>
            </form>
        </div>
        <div class="col-md-6 text-end">
            <a href="/batch_add" class="btn btn-success">
                <i class="bi bi-plus-circle"></i> 批量添加
            </a>
        </div>
    </div>

    <!-- 显示当前筛选状态 -->
    {% if account_filter %}
    <div class="alert alert-info mb-4">
        当前筛选账号: <strong>{{ account_filter }}</strong>
    </div>
    {% endif %}

    <!-- 统计卡片 -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card text-white bg-success mb-3">
                <div class="card-body">
                    <h5 class="card-title">今天/明天余分</h5>
                    <p class="card-text h2">{{ total_balance }}/{{ tomorrow_total_balance }}</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card text-white bg-info mb-3">
                <div class="card-body">
                    <h5 class="card-title">交易量积分</h5>
                    <p class="card-text h2">{{ total_transaction }}</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card text-white bg-danger mb-3">
                <div class="card-body">
                    <h5 class="card-title">消耗积分</h5>
                    <p class="card-text h2">{{ total_consumption }}</p>
                </div>
            </div>
        </div>
    </div>

    <!-- 今日积分记录 -->
    <h3>今日积分记录 ({{ today }})</h3>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>账号</th>
                <th>余额积分</th>
                <th>交易量积分</th>
                <th>消耗积分</th>
                <th>备注</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for score in today_scores %}
            <tr>
                <td>{{ score.account }}</td>
                <td class="{% if score.balance == 0 %}text-muted fst-italic{% endif %}">
                    {{ score.balance }}
                </td>
                <td class="{% if score.transaction == 0 %}text-muted fst-italic{% endif %}">
                    {{ score.transaction }}
                    {% if score.transaction == 0 %}
                        <span class="badge bg-warning ms-1">需关注</span>
                    {% endif %}
                </td>
                <td class="{% if score.consumption == 0 %}text-muted fst-italic{% endif %}">
                    {{ score.consumption }}
                    {% if score.consumption > 0 %}
                        <span class="badge bg-warning ms-1">需关注</span>
                    {% endif %}
                </td>
                <td>{{ score.remark }}</td>
                <td>
                    <a href="/edit/{{ score.id }}" class="btn btn-sm btn-primary">编辑</a>
                    <a href="/delete/{{ score.id }}" class="btn btn-sm btn-danger" onclick="return confirm('确定删除吗？')">删除</a>
                </td>
            </tr>
            {% else %}
            <tr>
                <td colspan="6" class="text-center">今日无积分记录</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    <!-- 回分补分提示 -->
    {% if recovery_records %}
    <h3 class="mt-5">即将回分/补分提示</h3>
    <div class="alert alert-warning">
        注意：以下记录将在未来3天内移出统计范围
        {% if nearest_expiry %}
            (最近移出日期: {{ nearest_expiry }})
        {% endif %}
    </div>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>日期</th>
                <th>账号</th>
                <th>余额积分</th>
                <th>交易量积分</th>
                <th>消耗积分</th>
                <th>备注</th>
                <th>移出日期</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for score in recovery_records %}
            <tr>
                <td>{{ score.date }}</td>
                <td>{{ score.account }}</td>
                <td class="{% if score.balance == 0 %}text-muted fst-italic{% endif %}">
                    {{ score.balance }}
                </td>
                <td>
                    <span class="{% if score.transaction == 0 %}text-muted fst-italic{% endif %}">
                        {{ score.transaction }}
                    </span>
                    {% if score.transaction == 0 %}
                        <span class="badge bg-danger ms-1">将补分</span>
                    {% endif %}
                </td>
                <td>
                    <span class="{% if score.consumption == 0 %}text-muted fst-italic{% endif %}">
                        {{ score.consumption }}
                    </span>
                    {% if score.consumption > 0 %}
                        <span class="badge bg-info ms-1">将回分</span>
                    {% endif %}
                </td>
                <td>{{ score.remark }}</td>
                <td>{{ score.expiry_date }}</td>
                <td>
                    <a href="/edit/{{ score.id }}" class="btn btn-sm btn-primary">编辑</a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% endif %}

    <!-- 15天内积分记录 -->
    <h3 class="mt-5">15天内积分记录</h3>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>日期</th>
                <th>账号</th>
                <th>余额积分</th>
                <th>交易量积分</th>
                <th>消耗积分</th>
                <th>备注</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for score in scores %}
            <tr>
                <td>{{ score.date }}
                 <span class="badge bg-info ms-1">{{ score.date | add_days(16) }}</span>
                </td>
                <td>{{ score.account }}</td>
                <td class="{% if score.balance == 0 %}text-muted fst-italic{% endif %}">
                    {{ score.balance }}
                </td>
                <td class="{% if score.transaction == 0 %}text-muted fst-italic{% endif %}">
                    {{ score.transaction }}
                    {% if score.transaction == 0 %}
                        <span class="badge bg-warning ms-1">需关注</span>
                    {% endif %}
                </td>
                <td class="{% if score.consumption == 0 %}text-muted fst-italic{% endif %}">
                    {{ score.consumption }}
                    {% if score.consumption > 0 %}
                        <span class="badge bg-warning ms-1">需关注</span>
                    {% endif %}
                </td>
                <td>{{ score.remark }}</td>
                <td>
                    <a href="/edit/{{ score.id }}" class="btn btn-sm btn-primary">编辑</a>
                    <a href="/delete/{{ score.id }}" class="btn btn-sm btn-danger" onclick="return confirm('确定删除吗？')">删除</a>
                </td>
            </tr>
            {% else %}
            <tr>
                <td colspan="7" class="text-center">无积分记录</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    <style>
        /* 为0值添加特殊样式 */
        .text-muted.fst-italic {
            color: #999 !important;
            font-style: italic;
        }
        /* 为需关注项添加间距 */
        .badge.ms-1 {
            margin-left: 0.25rem;
        }
    </style>
{% endblock %}